package com.grape.juc.safe.threadlocal;
/**
 * 描述:threadlocal测试
 * @author: myx
 * @date: 2019-05-08
 * Copyright © 2019-grape. All rights reserved.
 */
public class ThreadLocalTest extends Thread {
	private Res res;

	public ThreadLocalTest(Res res) {
		this.res = res;
	}

	@Override
	public void run() {
		for (int i = 0; i < 3; i++) {
			System.out.println(Thread.currentThread().getName() + "---" + "i---" + i + "--num:" + res.getNum());
		}
	}

	public static void main(String[] args) {
		Res res = new Res();
		ThreadLocalTest threadLocaDemo1 = new ThreadLocalTest(res);
		ThreadLocalTest threadLocaDemo2 = new ThreadLocalTest(res);
		ThreadLocalTest threadLocaDemo3 = new ThreadLocalTest(res);
		threadLocaDemo1.start();
		threadLocaDemo2.start();
		threadLocaDemo3.start();
	}
}


class Res {
	// 生成序列号共享变量
	public static Integer count = 0;
	public static ThreadLocal<Integer> threadLocal = new ThreadLocal<Integer>() {
		@Override
		protected Integer initialValue() {
			return 0;
		};

	};

	public Integer getNum() {
		int count = threadLocal.get() + 1;
		threadLocal.set(count);
		return count;
	}
}